angular.module('mvcApp.category-admin', [
    'ngRoute',
    'ngResource'
]).config(['$routeProvider', function($routeProvider) {
    $routeProvider
        .when('/category-admin', {
            templateUrl: 'static/views/category-admin.html',
            controller: 'CategoryAdminCtrl'
        });
}]).controller('CategoryAdminCtrl', ['$scope', '$http', 'Category', 'Shop', 'Product', 'User',
    function ($scope, $http, Category, Shop, Product, User) {
        $scope.currentPage = 'list';
        $scope.shopCategories = [];
        $scope.loadShopCategories = function() {
            Category.listShopCategories()
                .then(function (data) {
                    $scope.shopCategories = data;
                    $scope.$apply();
                }).catch(function (e) {
                    console.log(e);
                });
        };
        $scope.loadShopCategories();
        $scope.toAddCategory = function() {
            $scope.currentPage = 'create';
        };
        $scope.toShowCategories = function() {
            $scope.currentPage = 'list';
        };
        $scope.newShopCategory = {};
        $scope.onUploadedNewShopCategory = function(fileId) {
            var url = '/wechatshop/file/view_direct/' + encodeURIComponent(fileId);
            $scope.newShopCategory.logoUrl = url;
            $scope.$applyAsync();
        };
        $scope.saveShopCategory = function(shopCategory) {
            if(!shopCategory.name || !shopCategory.name.trim()) {
                return;
            }
            Category.createShopCategory(shopCategory.name, shopCategory.logoUrl)
                .then(function(data) {
                    $scope.toShowCategories();
                    $scope.loadShopCategories();
                    window.location.reload();
                }).catch(function(err) {
                    alert(err);
                });
            shopCategory.name = '';
            shopCategory.logoUrl = '';
        };
        $scope.myShops = [];
        $scope.loadMyShops = function () {
            Shop.getMyShops()
                .then(function (shops) {
                    $scope.myShops = shops;
                    $scope.$apply();
                }).catch(function (err) {
                    if (err) {
                        alert(err);
                    }
                });
        };
        $scope.loadMyShops();
        $scope.shopChanges = [];
        $scope.loadShopChanges = function () {
            Shop.listShopChanges(1, 10)
                .then(function (shopChanges, total) {
                    $scope.shopChanges = shopChanges;
                    $scope.$applyAsync();
                }).catch(function (err) {
                    if (err) {
                        console.log(err);
                    }
                });
        };
        $scope.loadShopChanges();
        $scope.agreeShopChange = function (shopChange) {
            Shop.agreeShopChange(shopChange.id)
                .then(function () {
                    alert("审核完成");
                    $scope.loadShopChanges();
                    $scope.loadMyShops();
                }).catch(function (err) {
                    alert(err);
                });
        };
        $scope.disagreeShopChange = function (shopChange) {
            Shop.disagreeShopChange(shopChange.id)
                .then(function () {
                    alert("拒绝审核完成");
                    $scope.loadShopChanges();
                }).catch(function (err) {
                    alert(err);
                });
        };

        $scope.productChanges = [];
        $scope.loadProductChanges = function () {
            Product.listProductChanges(1, 10)
                .then(function (productChanges, total) {
                    $scope.productChanges = productChanges;
                    $scope.$applyAsync();
                }).catch(function (err) {
                    if (err) {
                        console.log(err);
                    }
                });
        };
        $scope.loadProductChanges();
        $scope.agreeProductChange = function (productChange) {
            Product.agreeProductChange(productChange.id)
                .then(function () {
                    alert("审核完成");
                    $scope.loadProductChanges();
                }).catch(function (err) {
                    alert(err);
                });
        };
        $scope.disagreeProductChange = function (productChange) {
            Product.disagreeProductChange(productChange.id)
                .then(function () {
                    alert("拒绝审核完成");
                    $scope.loadProductChanges();
                }).catch(function (err) {
                    alert(err);
                });
        };
        $scope.index_background_image = 'http://p2.wmpic.me/article/2015/05/08/1431048328_BXxjuoyp.gif';
        $scope.index_background_images = [];
        function loadIndexBackgroundImage() {
            User.getConfig('index_background_image')
                .then(function (url) {
                    if (url) {
                        $scope.index_background_image = url;
                        $scope.index_background_images = url.split(',');
                        $scope.$applyAsync();
                    }
                }).catch(function (err) {
                    console.log(err);
                });
        }
        
        function loadIndexHotestShopsCount() {
            User.getConfig('index_hotest_shops_count')
                .then(function(count){
                    $scope.index_hotest_shops_count = parseInt(count);
                    $scope.$applyAsync();
                }).catch(function() {
                    $scope.index_hotest_shops_count = 5;
                    $scope.$applyAsync();
                });
        }
        loadIndexHotestShopsCount();

        loadIndexBackgroundImage();
        $scope.updateIndexBackgroundImagesConfig = function() {
            User.setConfig('index_background_image', $scope.index_background_images.join(','))
                .then(function () {
                    loadIndexBackgroundImage();
                }).catch(function (err) {
                    console.log(err);
                });
        };
        $scope.onUploadedIndexBackgroundColor = function (fileId) {
            var url = '/wechatshop/file/view_direct/' + encodeURIComponent(fileId);
            $scope.index_background_image = url;
            $scope.index_background_images.push(url);
            $scope.updateIndexBackgroundImagesConfig();
        };
        $scope.saveConfig = function (key, value) {
            User.setConfig(key, value)
                .then(function () {
                    alert("保存成功");
                }).catch(function (err) {
                    alert(err);
                });
        };
    }]);